<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/12
 * Time: 下午2:07
 */

function rebuildArr ($arr1 , $arr2=[])
{
    $tmp_arr1 = $arr1;
    if ( empty($arr2) ){
        $tmp_arr2 = $tmp_arr1;
    }else {
        $tmp_arr2 = $arr2;
    }
    foreach ( $tmp_arr1 as $key1 => $value1) {
        foreach ($tmp_arr2 as $key2 => $value2) {
            if ($value1['id'] == $value2['pid']) {
                $arr1[$key1]['son'][] = $value2;
            }
        }
        if (isset($arr1[$key1]['son'] )) {
            $arr1[$key1]['son'] = rebuildArr($arr1[$key1]['son'] , $arr1);
        }
    }

    return $arr1;
}

function rebuildArr2 ($arr1 , $arr2=[]) {
    $arr = rebuildArr($arr1 , $arr2=[]);
    foreach ($arr as $key=> $item) {
        if ($item['pid']!=0)
            unset($arr[$key]);
    }
    return $arr;
}
